package com.fingard.dsp.bank.directbank.boc01;


import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

import java.util.Date;
@Component("BOC01SinglePay")
public class BOC01SinglePay extends BOC01Base implements ISinglePay {

    /**
     * 单笔支付，不需采用流处理方式
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        if ("190810".equals(p_req1908.reqHeader.commandCode)) {//境内外币支付
            tmpRetDetail = d_190810_b2e0062(p_req1908);
        } else if ("190811".equals(p_req1908.reqHeader.commandCode)) {//跨境外币支付
            tmpRetDetail = d_190811_b2e0200(p_req1908);
        } else if ("190801".equals(p_req1908.reqHeader.commandCode)) {//上划下拨
            tmpRetDetail = d_190801_b2e0019(p_req1908);
        } else if ("190804".equals(p_req1908.reqHeader.commandCode)) {//上划下拨
            tmpRetDetail = d_190801_b2e0251(p_req1908);
        } else if ("190805".equals(p_req1908.reqHeader.commandCode)) {//虚拟户支付
            tmpRetDetail = d_190805_b2e0017(p_req1908);
        } else if ("1908CNVRT".equals(p_req1908.reqHeader.commandCode)) {
            tmpRetDetail = d_1908CNVRT_b2e0078(p_req1908);
        } else {
            if (p_req1908.singleDetail.isPrivate.equals("1")) {
                tmpRetDetail = d_1908_b2e0061(p_req1908);
            } else {
                tmpRetDetail = d_1908_b2e0009(p_req1908);
            }
        }
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    //d_190805_b2e0017 虚拟户支付
    private RetAtsSglDetail d_190805_b2e0017(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0017";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            //Date tmpDateNow = new Date();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
            xmlWriter.writeElementClosed("ceitinfo");
            xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
            xmlWriter.writeElementString("insid", p_req1908.singleDetail.askNumber);
            xmlWriter.writeElementString("grpidt", getBankConfig().getValueAsString("grpidt"));//集团号

            xmlWriter.writeStartElement("fractn");
            xmlWriter.writeElementString("fribkn", "");
            xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payHigherLevelAct);//资金池账号
            xmlWriter.writeEndElement();//fractn

            xmlWriter.writeStartElement("mdactn");
            xmlWriter.writeElementString("mdibkn", "");//名义付款人账户联行号
            xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payAct);//名义付款人账号
            xmlWriter.writeEndElement();//mdactn

            xmlWriter.writeStartElement("toactn");
            xmlWriter.writeElementString("toibkn", p_req1908.singleDetail.cnapsCode);//收款行联行号
            xmlWriter.writeElementString("actacn", p_req1908.singleDetail.recAct);//收款账号
            xmlWriter.writeElementString("toname", p_req1908.singleDetail.recName);//收款人名称
            xmlWriter.writeElementString("toaddr", p_req1908.singleDetail.recNameAddress);//收款人地址
            xmlWriter.writeElementString("tobknm", p_req1908.singleDetail.cnapsName);//收款人开户行名称
            xmlWriter.writeEndElement();//toactn

            xmlWriter.writeElementString("trnamt", p_req1908.singleDetail.payAmount);
            xmlWriter.writeElementString("trncur", "CNY");//只支持001或者CNY
            xmlWriter.writeElementString("priolv", p_req1908.singleDetail.paySpd);//银行处理优先级 0-普通；1-加急
            xmlWriter.writeElementString("cuspriolv", p_req1908.singleDetail.paySpd);//银行处理优先级 0-普通；1-加急
            xmlWriter.writeElementString("useinf", p_req1908.singleDetail.purpose);
            xmlWriter.writeElementString("furinfo", p_req1908.singleDetail.abstractATS);
            xmlWriter.writeElementString("trfdate", p_req1908.singleDetail.payDate.replaceAll("-", ""));//付款日期
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                            retDetail.respCode = "0";
                            retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                            retDetail.payInfoCode = retDetail.transState;
                            retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                            retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                        } else {
                            retDetail.respCode = "0";
                            retDetail.transState = tmprspcod;
                            retDetail.payInfoCode = tmprspcod;
                            retDetail.respInfo = tmprspmsg;
                            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.transState = tmprspcod;
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retDetail.respInfo = tmprspmsg;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = "虚拟户支付时出现异常！";
            WriteBankLogLn("虚拟户支付时出现异常,异常信息：" + e.getMessage());
        }
        return retDetail;
    }

    //d_1908_b2e0061 行内对私
    private RetAtsSglDetail d_1908_b2e0061(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0061";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        if (StringHelper.hasAnyChar(getBankConfig().getValueAsString("transtype"))) {
            xmlWriter.writeElementString("transtype", getBankConfig().getValueAsString("transtype"));
        } else {
            xmlWriter.writeElementClosed("transtype");
        }

        XElement tmpXElement = new XElement(bankHeader.trncod + "-rq", "");
        tmpXElement.addChild(new XElement("insid", p_req1908.singleDetail.askNumber));

        XElement fractnXElement = new XElement("fractn", "");
        fractnXElement.addChild(new XElement("fribkn", ""));
        fractnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.payAct));
        fractnXElement.addChild(new XElement("actnam", p_req1908.singleDetail.payName));
        tmpXElement.addChild(fractnXElement);

        XElement toactnXElement = new XElement("toactn", "");
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            toactnXElement.addChild(new XElement("toibkn", ""));
        } else {
            toactnXElement.addChild(new XElement("toibkn", p_req1908.singleDetail.cnapsCode));
        }
        if (p_req1908.singleDetail.cardType.equals("1")) {
            toactnXElement.addChild(new XElement("acttyp", "188"));
        } else {
            toactnXElement.addChild(new XElement("acttyp", ""));
        }
        toactnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.recAct));
        toactnXElement.addChild(new XElement("toname", p_req1908.singleDetail.recName));
        toactnXElement.addChild(new XElement("tobknm", p_req1908.singleDetail.getOppBankLocation()));
        toactnXElement.addChild(new XElement("toaddr", getBankConfig().getValueAsString("defaultAddr")));
        tmpXElement.addChild(toactnXElement);

        tmpXElement.addChild(new XElement("trnamt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("trncur", this.convertCurToBank(p_req1908.singleDetail.payCur)));

        if (p_req1908.singleDetail.paySpd.equals("1")) {
            tmpXElement.addChild(new XElement("priolv", "1"));
            tmpXElement.addChild(new XElement("cuspriolv", "1"));
        } else {
            tmpXElement.addChild(new XElement("priolv", "0"));
            tmpXElement.addChild(new XElement("cuspriolv", "0"));
        }

        tmpXElement.addChild(new XElement("furinfo", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("trfdate", Format.Date8Format.format(p_req1908.singleDetail.getPayDateTime())));
        tmpXElement.addChild(new XElement("comacn", p_req1908.singleDetail.payAct));
        //xmlWriter.writeElementClosed("trftime");

        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            tmpXElement.addChild(new XElement("bocflag", "1"));
        } else {
            tmpXElement.addChild(new XElement("bocflag", "0"));
        }

        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        //XElement tmpXElement = new XElement("DCOPDPAYX","");
        //tmpXElement.addChild(new XElement("YURREF",p_req1908.singleDetail.askNumber));
        //xmlWriter.writeXElement(tmpXElement);

        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.transState = tmprspcod;
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //d_1908_b2e0009 对公
    private RetAtsSglDetail d_1908_b2e0009(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0009";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        xmlWriter.writeElementString("transtype", getBankConfig().getValueAsString("transtype"));

        XElement tmpXElement = new XElement(bankHeader.trncod + "-rq", "");
        tmpXElement.addChild(new XElement("insid", p_req1908.singleDetail.askNumber));

        XElement fractnXElement = new XElement("fractn", "");
        fractnXElement.addChild(new XElement("fribkn", p_req1908.singleDetail.payArea));
        fractnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.payAct));
        fractnXElement.addChild(new XElement("actnam", p_req1908.singleDetail.payName));
        tmpXElement.addChild(fractnXElement);

        XElement toactnXElement = new XElement("toactn", "");
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            toactnXElement.addChild(new XElement("toibkn", p_req1908.singleDetail.recArea));
        } else {
            toactnXElement.addChild(new XElement("toibkn", p_req1908.singleDetail.cnapsCode));
        }
        toactnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.recAct));
        toactnXElement.addChild(new XElement("toname", p_req1908.singleDetail.recName));
        toactnXElement.addChild(new XElement("toaddr", ""));
        toactnXElement.addChild(new XElement("tobknm", p_req1908.singleDetail.getOppBankLocation()));
        tmpXElement.addChild(toactnXElement);

        tmpXElement.addChild(new XElement("trnamt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("trncur", this.convertCurToBank(p_req1908.singleDetail.payCur)));
        if (p_req1908.singleDetail.paySpd.equals("1")) {
            tmpXElement.addChild(new XElement("priolv", "1"));
        } else {
            tmpXElement.addChild(new XElement("priolv", "0"));
        }

        tmpXElement.addChild(new XElement("furinfo", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("trfdate", Format.Date8Format.format(p_req1908.singleDetail.getPayDateTime())));
        //xmlWriter.writeElementClosed("trftime");
        tmpXElement.addChild(new XElement("comacn", p_req1908.singleDetail.payAct));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        //XElement tmpXElement = new XElement("DCOPDPAYX","");
        //tmpXElement.addChild(new XElement("YURREF",p_req1908.singleDetail.askNumber));
        //xmlWriter.writeXElement(tmpXElement);

        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0009-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0009-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-b2e0009-rs/b2e0009-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    private RetAtsSglDetail d_190810_b2e0062(ReqSingle p_req190810) throws Exception {//境内外币汇款申请
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0062";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-b2e0062-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        xmlWriter.writeElementString("transtype", "");
        xmlWriter.writeStartElement("b2e0062-rq");
        xmlWriter.writeElementString("insid", p_req190810.singleDetail.askNumber);
        xmlWriter.writeElementString("obssid", "");
        xmlWriter.writeStartElement("fractn");
        /*Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
		String date =  Format.Date8Format.format(calendar.getTime());*/
        xmlWriter.writeElementString("trfdate", Format.Date8Format.format(new Date()));//非空日期YYYYMMDD 系统当前日期（含当日）之后的一个月内
        xmlWriter.writeElementString("trntyp", "T");//<trntyp>非空枚举（目前只包 括电汇 ：T)</trntyp>
        xmlWriter.writeElementString("trnamt", p_req190810.singleDetail.payAmount);
        xmlWriter.writeElementString("trncur", convertCurToBank(p_req190810.singleDetail.payCur));
        xmlWriter.writeElementString("fribkn", p_req190810.singleDetail.payArea);//非空五位
        xmlWriter.writeElementString("cexact", p_req190810.singleDetail.payAct);//现汇账户
        xmlWriter.writeElementString("cexamt", p_req190810.singleDetail.payAmount);//现汇金额
        xmlWriter.writeElementString("puract", "");//购汇账户
        xmlWriter.writeElementString("puramt", "");//购汇金额
        xmlWriter.writeElementString("othact", "");
        xmlWriter.writeElementString("othamt", "");
        xmlWriter.writeElementString("feeact", p_req190810.singleDetail.payAct);//付费账户
        xmlWriter.writeElementString("frname", p_req190810.singleDetail.payName);//汇款人名称
        String fraddr = getBankConfig().getMapValueByKey("fraddrMapping", p_req190810.singleDetail.payAct);
        if (StringHelper.isNullOrEmpty(fraddr)) {
            fraddr = p_req190810.singleDetail.payAreaName;
        }
        xmlWriter.writeElementString("fraddr", fraddr);
        xmlWriter.writeElementString("orgcde", getBankConfig().getValueAsString("orgcde"));//组织机构代码 非空10位字need
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("toactn");//收款人账户信息
        String sameBnk = "";
        if (p_req190810.singleDetail.sameBnk.equals("1")) {
            sameBnk = "1";
            xmlWriter.writeElementString("bedtyp", sameBnk);
            xmlWriter.writeElementString("toibkn", p_req190810.singleDetail.recArea);//收款行联行号 中行必填五位数
        } else {
            sameBnk = "0";
            xmlWriter.writeElementString("bedtyp", sameBnk);
            xmlWriter.writeElementString("toibkn", "");
        }
        xmlWriter.writeElementString("bedlnm", "");
        xmlWriter.writeElementString("bedadr", "");
        String beaknm = p_req190810.singleDetail.recBankLocation;
        if (StringHelper.isNullOrEmpty(beaknm)) {
            beaknm = p_req190810.singleDetail.recBankName;
        }
        xmlWriter.writeElementString("beaknm", beaknm);
        String beaadr = p_req190810.singleDetail.recBankAddress;
        if (StringHelper.isNullOrEmpty(beaadr)) {
            beaadr = p_req190810.singleDetail.recBankLocation;
        }
        xmlWriter.writeElementString("beaadr", beaadr);
        xmlWriter.writeElementString("bdlact", "");
        xmlWriter.writeElementString("toname", p_req190810.singleDetail.recName);//收款人名称 need
        xmlWriter.writeElementString("toaddr", p_req190810.singleDetail.recNameAddress);
        xmlWriter.writeElementString("actacn", p_req190810.singleDetail.recAct);//收款账号 need  农行账号需要特殊处理？
        xmlWriter.writeEndElement();
        xmlWriter.writeElementString("furinfo", p_req190810.singleDetail.purpose + "-" + p_req190810.singleDetail.abstractATS);
        xmlWriter.writeElementString("feemode", "SHA");
        //Todo Ats提供值传入功能 config
        String coucde = p_req190810.singleDetail.coucde;
        if (StringHelper.isNullOrEmpty(coucde)) {
            xmlWriter.writeElementString("coucde", "156");//need 代码参考附件七 收款人常驻国家代码
        } else {
            xmlWriter.writeElementString("coucde", coucde);
        }
        //默认其他
        xmlWriter.writeElementString("paytyp", "O");//选输，付款方式：A预付货款、P货到付款、R退款、O其他包括预付货款、货到付款、退款、其他
        String paySpd = "";
        if ("1".equals(p_req190810.singleDetail.paySpd)) {
            paySpd = "U";
        } else {
            paySpd = "N";
        }
        xmlWriter.writeElementString("priolv", paySpd);//NEED 发电等级 非空枚举（N普通、U加急）
        //默认为否
        xmlWriter.writeElementString("canflg", "N");//是否为报税货物项下付款 Y是N否
        //暂定其他
        xmlWriter.writeElementString("rettyp", "5");//付汇性质包括保税区0、出口加工区1、钻石交易所、深加工结转4、其它5其他特殊经济区域 6
        xmlWriter.writeElementString("lstdat", "");
        //一批就一笔 给个默认值
        xmlWriter.writeElementString("trncd1", "422000");//交易编码1
        xmlWriter.writeElementString("trnam1", p_req190810.singleDetail.payAmount);//相应金额1
        xmlWriter.writeElementString("trncd2", "");
        xmlWriter.writeElementString("trnam2", "");
        xmlWriter.writeElementString("comnum", "");
        xmlWriter.writeElementString("invnum", "");
        xmlWriter.writeElementString("whbnum", "");
        xmlWriter.writeElementString("comcde", "");
        xmlWriter.writeElementString("pernam", "");
        xmlWriter.writeElementString("pertel", "");

        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0062-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0062-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-b2e0062-rs/b2e0062-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req190810.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req190810.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    private RetAtsSglDetail d_190811_b2e0200(ReqSingle p_req190811) throws Exception {//跨境汇款申请
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0200";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-b2e0200-rq");
        xmlWriter.writeElementString("TRANS_TYPE", "");
        xmlWriter.writeStartElement("b2e0200-rq");
        xmlWriter.writeElementString("OBSS_ID", "");
        xmlWriter.writeElementString("AUTH_EXPIRY_DT", Format.Date10Format.format(new Date()));//1、客户标示为TC时必输2、非空数字格式为yyyy-MM-dd
        xmlWriter.writeElementString("REM_MODE", "S");//S-T/T电汇（默认）F-D/D票汇L-M/T信汇
        if ("1".equals(p_req190811.singleDetail.paySpd)) {
            xmlWriter.writeElementString("RET_LVL", "2");//1: 普通（默认）2: 加急
        } else {
            xmlWriter.writeElementString("RET_LVL", "1");
        }
        xmlWriter.writeElementString("TRN_AMT", p_req190811.singleDetail.payAmount);
        xmlWriter.writeElementString("TRN_CUR", convertCurToBank(p_req190811.singleDetail.payCur));//汇款币种 非空大写字母只支持
        xmlWriter.writeElementString("CEX_AMT", p_req190811.singleDetail.payAmount);//现汇金额
        xmlWriter.writeElementString("CEX_ACNO", p_req190811.singleDetail.payAct);//现汇账号 1、 可空数字，字母0-34位2、 如费用交付方式选择3-帐户则为必填必输项
        xmlWriter.writeElementString("PUR_AMT", "");
        xmlWriter.writeElementString("PUR_ACNO", "");
        xmlWriter.writeElementString("OTR_AMT", "");
        xmlWriter.writeElementString("OTR_ACNO", "");
        xmlWriter.writeElementString("FEE_ACNO", "");//1、 可空数字，字母0-34位2、 如费用交付方式选择3-帐户则为必填必输项
        xmlWriter.writeElementString("FRN_NAME_50A", p_req190811.singleDetail.payName);
        String payAddr = getBankConfig().getMapValueByKey("payAddrMapping", p_req190811.singleDetail.payAct);
        if (StringHelper.isNullOrEmpty(payAddr)) {
            payAddr = p_req190811.singleDetail.getOppAreaName();
        }
        xmlWriter.writeElementString("FRN_ADDRESS_50A", payAddr);
        String orgCode = p_req190811.singleDetail.payOrgCode;
        if (StringHelper.isNullOrEmpty(orgCode)) {
            orgCode = bankFront.org;
        }
        xmlWriter.writeElementString("ORG_CDE", orgCode);//组织机构代码
        xmlWriter.writeElementString("BEDL_NAME_56A", "");
        xmlWriter.writeElementString("BEDL_ADDRESS_56A", "");
        xmlWriter.writeElementString("BDL_ACT_57A", "");
        xmlWriter.writeElementString("BEAK_NAME", p_req190811.singleDetail.recBankLocation);//57a收款银行名称
        String beakAddress = p_req190811.singleDetail.recBankAddress;
        if (StringHelper.isNullOrEmpty(beakAddress)) {
            beakAddress = p_req190811.singleDetail.recBankLocation;
        }
        xmlWriter.writeElementString("BEAK_ADDRESS", beakAddress);
        xmlWriter.writeElementString("TO_ACT_ACN_59A", p_req190811.singleDetail.recAct);//59a收款人帐 号
        xmlWriter.writeElementString("TO_NAME_59A", p_req190811.singleDetail.recName);
        xmlWriter.writeElementString("TO_ADDRESS_59A", p_req190811.singleDetail.recNameAddress);
        xmlWriter.writeElementString("POSTSCRIPTMT", p_req190811.singleDetail.purpose + "-" + p_req190811.singleDetail.abstractATS);
        xmlWriter.writeElementString("FEE_MODE", "SHA");
        //收款人常驻国家(地区)代码 非空数字3位参见附录三备注：屏蔽制裁国家364（伊朗）,760（叙利亚）,736（苏丹）,192（古巴）,408（朝鲜）
        //先给个默认值356 印度
        String coucde = p_req190811.singleDetail.coucde;
        if (StringHelper.isNullOrEmpty(coucde)) {
            xmlWriter.writeElementString("COU_CDE", "356");//收款人常驻国家(地区)代码
        } else {
            xmlWriter.writeElementString("COU_CDE", coucde);
        }
        xmlWriter.writeElementString("PAY_TYPE", "4");//付款方式 1-预付货款2-货到付款3-退款4-其他
        //交易金额、编码、币别、附言都可以为空 先不理会
        xmlWriter.writeElementString("TRN_CD1", "");
        xmlWriter.writeElementString("TRN_CUR1", "");
        xmlWriter.writeElementString("TRN_AMT1", "");
        xmlWriter.writeElementString("TRN_RE1", "");
        xmlWriter.writeElementString("TRN_CD2", "");
        xmlWriter.writeElementString("TRN_CUR2", "");
        xmlWriter.writeElementString("TRN_AMT2", "");
        xmlWriter.writeElementString("TRN_RE2", "");
        //默认为0
        xmlWriter.writeElementString("IS_BGD_PAY", "0");//本币款项是否为保税货物项下付款 可空枚举0或1；0为“否”1为“是”
        xmlWriter.writeElementString("COM_NUM", "");
        xmlWriter.writeElementString("INV_NUM", "");
        xmlWriter.writeElementString("WHB_NUM", "");
        //姓名先取付方账号、电话先给默认值吧  应该由ATS上送先作为可配置化吧
        xmlWriter.writeElementString("PER_NAME", p_req190811.singleDetail.payName);//申请人姓名 need
        xmlWriter.writeElementString("PER_TEL", getBankConfig().getValueAsString("pertel"));//申请人联系电话 need
        xmlWriter.writeElementString("ERP_REF", p_req190811.singleDetail.askNumber);//集团业务编号
        //如何区分国内国外 根据联行号还是开户行所在国家 先提供默认值吧  由ATS上送？
        xmlWriter.writeElementString("REM_TYP", "1");//汇款种类 need 非空 1-国外汇款；2-国内汇款
        //与FEE_ACNO相关联 先默认为1 由ATS上送？
        xmlWriter.writeElementString("FEE_PAY_TYPE", "1");//<FEE_PAY_TYPE>费用交付方式可空枚举1、2、3，1-现金，2-支票，3-账户</FEE_PAY_TYPE>
        xmlWriter.writeElementString("IBK_NUM", "");//联行号 银行和数据库账号绑定做校验？
        //默认为BN  由ATS上送？
        xmlWriter.writeElementString("CUS_FLAG", "BN");//客户标识 need 非空 BN：网银WEBTC：中化TC
        xmlWriter.writeElementString("REMARK1", "");
        xmlWriter.writeElementString("REMARK2", "");
        xmlWriter.writeElementString("BED_BIC_57A", "");
        xmlWriter.writeElementString("TRF_DATE", Format.Date8Format.format(new Date()));
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0200-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0200-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-b2e0200-rs/b2e0200-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = p_req190811.singleDetail.askNumber;
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req190811.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req190811.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //上划下拨（内部调拨)
    private RetAtsSglDetail d_190801_b2e0019(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0019";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
        String payTypeCategory = p_req1908.singleDetail.primaryAct;
        payTypeCategory = "0".equals(payTypeCategory) ? "01" : "1".equals(payTypeCategory) ? "00" : "00";
        xmlWriter.writeElementString("trntyp", payTypeCategory);//need (00-下拨 01- 上划)
        xmlWriter.writeElementString("insid", p_req1908.singleDetail.askNumber);
        xmlWriter.writeElementString("grpidt", getBankConfig().getValueAsString("grpidt"));//集团号
        xmlWriter.writeStartElement("fractn");
        xmlWriter.writeElementString("fribkn", p_req1908.singleDetail.payArea);
        xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payAct);
        xmlWriter.writeElementString("actnam", p_req1908.singleDetail.payName);
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("toactn");
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            xmlWriter.writeElementString("toibkn", p_req1908.singleDetail.recArea);
        } else {
            xmlWriter.writeElementString("toibkn", p_req1908.singleDetail.cnapsCode);
        }
        xmlWriter.writeElementString("actacn", p_req1908.singleDetail.recAct);
        xmlWriter.writeElementString("toname", p_req1908.singleDetail.recName);
        xmlWriter.writeElementString("tobknm", p_req1908.singleDetail.getOppBankLocation());
        xmlWriter.writeElementClosed("toaddr");

        xmlWriter.writeEndElement();
        xmlWriter.writeElementString("trnamt", p_req1908.singleDetail.payAmount);
        xmlWriter.writeElementString("trncur", this.convertCurToBank(p_req1908.singleDetail.payCur));
        if (p_req1908.singleDetail.paySpd.equals("1")) {
            xmlWriter.writeElementString("priolv", "1");
        } else {
            xmlWriter.writeElementString("priolv", "0");
        }
        xmlWriter.writeElementString("furinfo", p_req1908.singleDetail.abstractATS);
        xmlWriter.writeElementString("comacn", p_req1908.singleDetail.payAct);
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.transState = tmprspcod;
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    private RetAtsSglDetail d_190801_b2e0251(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0251";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");

        XElement tmpXElement = new XElement(bankHeader.trncod + "-rq", "");
        tmpXElement.addChild(new XElement("insid", p_req1908.singleDetail.askNumber));
        XElement fractnXElement = new XElement("fractn", "");
        fractnXElement.addChild(new XElement("fribkn", p_req1908.singleDetail.payArea));
        fractnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.payAct));
        fractnXElement.addChild(new XElement("actnam", p_req1908.singleDetail.payName));
        tmpXElement.addChild(fractnXElement);
        XElement toactnXElement = new XElement("toactn", "");
        boolean notUseToibkn = getBankConfig().getValueAsBool("NotUseToibkn");
        if (notUseToibkn) {
            //工单ATSES-24221 正大天晴中行b2e0251接口特殊逻辑
            toactnXElement.addChild(new XElement("toibkn", p_req1908.singleDetail.cnapsCode));
        } else {
            toactnXElement.addChild(new XElement("toibkn", getBankConfig().getMapValueByKey("toibknMapping", p_req1908.singleDetail.recBankCode)));
        }
        toactnXElement.addChild(new XElement("actacn", p_req1908.singleDetail.recAct));
        toactnXElement.addChild(new XElement("toname", p_req1908.singleDetail.recName));
        if (p_req1908.singleDetail.isPrivate.equals("1")) {
            toactnXElement.addChild(new XElement("type", "04"));
        } else {
            toactnXElement.addChild(new XElement("type", "00"));
        }
        tmpXElement.addChild(toactnXElement);

        XElement feeinfoXelement = new XElement("feeinfo", "");
        feeinfoXelement.addChild(new XElement("ibknum", p_req1908.singleDetail.payArea));
        feeinfoXelement.addChild(new XElement("actacn", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(feeinfoXelement);
        tmpXElement.addChild(new XElement("trnamt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("trncur", this.convertCurToBank(p_req1908.singleDetail.payCur)));
        if (p_req1908.singleDetail.paySpd.equals("1")) {
            tmpXElement.addChild(new XElement("priolv", "1"));
        } else {
            tmpXElement.addChild(new XElement("priolv", "0"));
        }
        tmpXElement.addChild(new XElement("furinfo", p_req1908.singleDetail.abstractATS));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());

//        xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
//        xmlWriter.writeElementString("insid", p_req1908.singleDetail.askNumber);
//        xmlWriter.writeStartElement("fractn");
//        xmlWriter.writeElementString("fribkn", p_req1908.singleDetail.payArea);
//        xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payAct);
//        xmlWriter.writeElementString("actnam", p_req1908.singleDetail.payName);
//        xmlWriter.writeEndElement();
//
//        xmlWriter.writeStartElement("toactn");
////        boolean notUseToibkn = getBankConfig().getValueAsBool("NotUseToibkn");
//        if (notUseToibkn) {
//            //工单ATSES-24221 正大天晴中行b2e0251接口特殊逻辑
//            xmlWriter.writeElementString("toibkn", p_req1908.singleDetail.cnapsCode);
//        } else {
//            xmlWriter.writeElementString("toibkn", getBankConfig().getMapValueByKey("toibknMapping", p_req1908.singleDetail.recBankCode));
//        }
//        xmlWriter.writeElementString("actacn", p_req1908.singleDetail.recAct);
//        xmlWriter.writeElementString("toname", p_req1908.singleDetail.recName);
//        if (p_req1908.singleDetail.isPrivate.equals("1")) {
//            xmlWriter.writeElementString("type", "04");
//        } else {
//            xmlWriter.writeElementString("type", "00");
//        }
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("feeinfo");
//        xmlWriter.writeElementString("ibknum", p_req1908.singleDetail.payArea);
//        xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payAct);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeElementString("trnamt", p_req1908.singleDetail.payAmount);
//        xmlWriter.writeElementString("trncur", this.convertCurToBank(p_req1908.singleDetail.payCur));
//        if (p_req1908.singleDetail.paySpd.equals("1")) {
//            xmlWriter.writeElementString("priolv", "1");
//        } else {
//            xmlWriter.writeElementString("priolv", "0");
//        }
//        xmlWriter.writeElementString("furinfo", p_req1908.singleDetail.abstractATS);
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                        retDetail.respCode = "0";
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                    } else {
                        retDetail.respCode = "0";
                        retDetail.transState = tmprspcod;
                        retDetail.payInfoCode = tmprspcod;
                        retDetail.respInfo = tmprspmsg;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.transState = tmprspcod;
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    // 一笔一批
    private RetAtsSglDetail d_1908CNVRT_b2e0078(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0078";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
            xmlWriter.writeElementClosed("ceitinfo");
            xmlWriter.writeElementClosed("transtype");
            xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
            xmlWriter.writeElementString("insid", p_req1908.singleDetail.askNumber);
            xmlWriter.writeStartElement("fractn");
            xmlWriter.writeElementString("fribkn", "");
            xmlWriter.writeElementString("actacn", p_req1908.singleDetail.payAct);
            xmlWriter.writeElementString("actnam", p_req1908.singleDetail.payName);
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("telephone", "");
            xmlWriter.writeElementString("pybcur", this.convertCurToBank(p_req1908.singleDetail.payCur));
            xmlWriter.writeElementString("pybamt", p_req1908.singleDetail.payAmount);
            xmlWriter.writeElementString("pybnum", "1");
            if (p_req1908.singleDetail.sameBnk.equals("1")) {//crdtyp 6：他行	7：我行
                xmlWriter.writeElementString("crdtyp", "7");
            } else {
                xmlWriter.writeElementString("crdtyp", "6");
            }
            String furinfo = getBankConfig().getMapValueByKey("furinfo" + bankHeader.trncod, p_req1908.singleDetail.useType);
            xmlWriter.writeElementString("furinfo", furinfo);
            String useinf = this.getBankConfig().getValueAsBool("unsetAbstr2b2e0078useinf") ? "" : p_req1908.singleDetail.abstractATS;
            xmlWriter.writeElementString("useinf", useinf);
            xmlWriter.writeElementString("trfdate", Format.Date8Format.format(new Date()));
            XElement tmpXElement = new XElement("detail", "");
            String area = p_req1908.singleDetail.recArea;
            String standardArea = p_req1908.singleDetail.recStandardArea;
            String areaMapped = "";
            if (StringHelper.hasAnyChar(area)) {
                areaMapped = getBankConfig().getMapValueByKey("areaMapping", area);
            }
            if (StringHelper.isNullOrEmpty(areaMapped)) {
                areaMapped = getBankConfig().getMapValueByKey("areaMapping", standardArea);
            }
            if ("1".equals(p_req1908.singleDetail.sameBnk)) {
                tmpXElement.addChild(new XElement("toibkn", areaMapped));
            } else {
                tmpXElement.addChild(new XElement("toibkn", p_req1908.singleDetail.cnapsCode));
            }
            tmpXElement.addChild(new XElement("tobank", p_req1908.singleDetail.getOppBankLocation()));
            tmpXElement.addChild(new XElement("toactn", p_req1908.singleDetail.recAct));
            tmpXElement.addChild(new XElement("pydcur", this.convertCurToBank(p_req1908.singleDetail.payCur)));
            tmpXElement.addChild(new XElement("pydamt", p_req1908.singleDetail.payAmount));
            tmpXElement.addChild(new XElement("toname", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("toidtp", ""));
            tmpXElement.addChild(new XElement("toidet", ""));
            tmpXElement.addChild(new XElement("furinfo", p_req1908.singleDetail.askNumber));
            tmpXElement.addChild(new XElement("purpose", ""));
            tmpXElement.addChild(new XElement("reserve1", ""));
            tmpXElement.addChild(new XElement("reserve2", ""));
            tmpXElement.addChild(new XElement("reserve3", ""));
            tmpXElement.addChild(new XElement("reserve4", ""));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                            retDetail.respCode = "0";
                            retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                            retDetail.payInfoCode = retDetail.transState;
                            retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        } else {
                            retDetail.respCode = "0";
                            retDetail.transState = tmprspcod;
                            retDetail.payInfoCode = tmprspcod;
                            retDetail.respInfo = tmprspmsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmprspmsg;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.transState = PaymentState.FAIL_Batch;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
            e.printStackTrace();
        }
        return retDetail;
    }

    /**
     * 状态查询 190810同1944接口 190811跨境查询
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        if ("1908CNVRT".equals(p_req1944.reqHeader.commandCode)) {
            return d_1944_b2e0079(p_req1944);
        }
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0007";//交易代码
        if (p_req1944.reqHeader.commandCode.equals("190811")) {
            bankHeader.trncod = "b2e0204";
        }
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
        if (p_req1944.reqHeader.commandCode.equals("190811")) {
            xmlWriter.writeElementString("ERP_REF", p_req1944.askNumber);
            xmlWriter.writeElementString("OBSS_ID", "");
        } else {
            xmlWriter.writeElementString("insid", p_req1944.askNumber);
            xmlWriter.writeElementClosed("obssid");
        }

        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        //retDetail.respCode = "0";
                        //retDetail.respInfo = tmpRetMsg;
                        //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                        if (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                            String tmpRetCode = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                            String tmpRetInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                            retDetail.respCode = "0";
                            if (p_req1944.reqHeader.commandCode.equals("190811")) {
                                //FAIL_REASON 01-客户资料提交不完全或不符合我行规定02-汇款金额有误03-账户及汇款人信息有误04-银行信息有误
                                //05-客户印鉴不符06-授权有效期内未办理07-其他B2-网银处理失败处理中：01：待处理02：已处理10：待经办
                                //11：通过待复核12：拒绝待复核B1:网银处理中
                                String FAIL_REASON = tmpBkReader.readedXElement.getSingleText("FAIL_REASON");
                                String FAIL_DESCRPT = tmpBkReader.readedXElement.getSingleText("FAIL_DESCRPT");
                                String APPLY_STATE = tmpBkReader.readedXElement.getSingleText("APPLY_STATE");//申请状态 申请状态(长度为2位；0-申请成功；1-申请失败;2-处理中)
                                if (FAIL_DESCRPT.length() > 0) {
                                    retDetail.payInfoCode = "FAIL_REASON+" + FAIL_REASON + "+" + FAIL_DESCRPT;
                                    ;
                                } else if (FAIL_REASON.length() > 0) {
                                    retDetail.payInfoCode = "FAIL_REASON+" + FAIL_REASON;
                                } else {
                                    retDetail.payInfoCode = tmpRetCode;
                                }
                                retDetail.transState = tmpRetCode + "+APPLY_STATE+" + APPLY_STATE;
                                retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("FAIL_DESCRPT");
                                if (StringHelper.isNullOrEmpty(retDetail.respInfo)) {
                                    retDetail.respInfo = tmpRetInfo;
                                }
                                retDetail.reqSeqID = p_req1944.askNumber;
                            } else {
                                retDetail.transState = tmpRetCode;
                                retDetail.payInfoCode = retDetail.transState;
                                retDetail.respInfo = tmpRetInfo;
                                retDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                            }
                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmprspmsg;
                            retDetail.reqSeqID = p_req1944.askNumber;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retDetail.respInfo = tmprspmsg;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    public RetAtsSingle d_1944_b2e0079(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1944);
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0079";
            bankHeader.trnid = getQryTransID();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
            xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
            xmlWriter.writeElementClosed("obssid");
            xmlWriter.writeElementString("insid", p_req1944.askNumber);
            xmlWriter.writeElementString("begnum", "");
            xmlWriter.writeElementString("recnum", "001");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                    if (StringHelper.hasAnyChar(tmprspcod)) {
                        String detailNode = "/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs";
                        int tmpCountRow = tmpBkReader.countElement(detailNode);
                        if (tmpCountRow > 0) {
                            if (tmpCountRow == 1) {
                                while (tmpBkReader.read(detailNode)) {
                                    String rspcode = tmpBkReader.getFirstText(detailNode + "/status/rspcod");
                                    String rspmsg = tmpBkReader.getFirstText(detailNode + "/status/rspmsg");
                                    retDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                                    retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                                    retDetail.payInfoCode = retDetail.transState;
                                    if ("B052".equals(rspcode)) {
                                        if (canBeNotExist(p_req1944, retDetail, 60)) {
                                            retDetail.respCode = "0";
                                            retDetail.respInfo = rspmsg;
                                            retDetail.transState = StringHelper.joinNotEmpty("+", rspcode, rspmsg);
                                            retDetail.payInfoCode = rspcode;
                                        }
                                    }
                                }
                            } else {
                                retDetail.respCode = "-1";
                                retDetail.respInfo = "银行返回笔数" + tmpCountRow + "与实际查询笔数不符";
                            }
                        } else if (tmpCountRow == 0) {
                            retDetail.respCode = "0";
                            retDetail.transState = tmprspcod + "+" + tmprspmsg;
                            retDetail.respInfo = tmprspmsg;
                            retDetail.payInfoCode = tmprspcod;
                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmprspmsg;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmprspmsg;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn(e);
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        if (!p_req1908BAT.hasPayPub()) {
            RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1908BAT.reqHeader);
            retAts.totalNum = p_req1908BAT.totalNum;
            retAts.totalAmt = p_req1908BAT.totalAmt;
            retAts.reqBatchNO = p_req1908BAT.reqBatchNO;
            retAts.batRespCode = "0";
            retAts.batRespInfo = "该批次内公私标志不一致，需保持一致！";
            retAts.batTransState = PaymentState.FAIL_Payment;
            retAts.batPayInfoCode = "E1301";
            retAts.writeBeforeDetail();
            retAts.writeAfterDetail();
            return retAts;
        }

        if (p_req1908BAT.isPrivate.equals("1")) {
            return d_1908BAT_b2e0061(p_req1908BAT);
        } else {
            return d_1908BAT_b2e0009(p_req1908BAT);
        }
    }

    //d_1908_b2e0061 行内对私
    private RetAtsSingleBat d_1908BAT_b2e0061(ReqSingleBat p_req1908BAT) throws Exception {
        RetAtsSingleBat retDetail = new RetAtsSingleBat(p_req1908BAT.reqHeader);
        retDetail.totalAmt = p_req1908BAT.totalAmt;
        retDetail.totalNum = p_req1908BAT.totalAmt;
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0061";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        if (StringHelper.hasAnyChar(getBankConfig().getValueAsString("transtype"))) {
            xmlWriter.writeElementString("transtype", getBankConfig().getValueAsString("transtype"));
        } else {
            xmlWriter.writeElementClosed("transtype");
        }
        while (p_req1908BAT.readRecord()) {
            ReqPayRecDetail tmpDetail = p_req1908BAT.readedDetail;
            XElement tmpXElement = new XElement(bankHeader.trncod + "-rq", "");
            tmpXElement.addChild(new XElement("insid", tmpDetail.askNumber));

            XElement fractnXElement = new XElement("fractn", "");
            fractnXElement.addChild(new XElement("fribkn", tmpDetail.payArea));
            fractnXElement.addChild(new XElement("actacn", tmpDetail.payAct));
            fractnXElement.addChild(new XElement("actnam", tmpDetail.payName));
            tmpXElement.addChild(fractnXElement);

            XElement toactnXElement = new XElement("toactn", "");
            if (tmpDetail.sameBnk.equals("1")) {
                toactnXElement.addChild(new XElement("toibkn", tmpDetail.recArea));
            } else {
                toactnXElement.addChild(new XElement("toibkn", tmpDetail.cnapsCode));
            }
            if (tmpDetail.cardType.equals("1")) {
                toactnXElement.addChild(new XElement("acttyp", "188"));
            } else {
                toactnXElement.addChild(new XElement("acttyp", ""));
            }
            toactnXElement.addChild(new XElement("actacn", tmpDetail.recAct));
            toactnXElement.addChild(new XElement("toname", tmpDetail.recName));
            toactnXElement.addChild(new XElement("tobknm", tmpDetail.getOppBankLocation()));
            toactnXElement.addChild(new XElement("toaddr", getBankConfig().getValueAsString("defaultAddr")));
            tmpXElement.addChild(toactnXElement);

            tmpXElement.addChild(new XElement("trnamt", tmpDetail.payAmount));
            tmpXElement.addChild(new XElement("trncur", this.convertCurToBank(tmpDetail.payCur)));

            if (tmpDetail.paySpd.equals("1")) {
                tmpXElement.addChild(new XElement("priolv", "1"));
                tmpXElement.addChild(new XElement("cuspriolv", "1"));
            } else {
                tmpXElement.addChild(new XElement("priolv", "0"));
                tmpXElement.addChild(new XElement("cuspriolv", "0"));
            }

            tmpXElement.addChild(new XElement("furinfo", tmpDetail.purpose));
            tmpXElement.addChild(new XElement("trfdate", Format.Date8Format.format(tmpDetail.getPayDateTime())));
            tmpXElement.addChild(new XElement("comacn", tmpDetail.payAct));
            //xmlWriter.writeElementClosed("trftime");

            if (tmpDetail.sameBnk.equals("1")) {
                tmpXElement.addChild(new XElement("bocflag", "1"));
            } else {
                tmpXElement.addChild(new XElement("bocflag", "0"));
            }

            nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);

            //XElement tmpXElement = new XElement("DCOPDPAYX","");
            //tmpXElement.addChild(new XElement("YURREF",p_req1908.singleDetail.askNumber));
            //xmlWriter.writeXElement(tmpXElement);
        }
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    retDetail.batRespCode = "0";
                    retDetail.batRespInfo = tmprspmsg;
                    retDetail.reqBatchNO = p_req1908BAT.reqBatchNO;
                    while (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                        RetAtsSglDetail tmpDetail = new RetAtsSglDetail();
                        tmpDetail.respCode = "0";
                        tmpDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        tmpDetail.payInfoCode = tmpDetail.transState;
                        tmpDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        tmpDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                        retDetail.writeRetDetail(tmpDetail);
                    }
                    if (retDetail.getWrittenRdCount() == 0) {
                        retDetail.batTransState = tmprspcod;
                        retDetail.batPayInfoCode = retDetail.batTransState;
                    }
                } else {
                    retDetail.batRespCode = "-1";
                    retDetail.batTransState = tmprspcod;
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.batRespInfo = tmprspmsg;
                }
            } else {
                retDetail.batRespCode = "-1";
                retDetail.batRespInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.batRespCode = "0";
            retDetail.batRespInfo = tmpStrRet[1];
            retDetail.reqBatchNO = p_req1908BAT.reqBatchNO;
            retDetail.batTransState = PaymentState.FAIL_Payment;
            retDetail.batPayInfoCode = tmpStrRet[0];
        } else {
            retDetail.batRespCode = "-1";
            retDetail.batRespInfo = tmpStrRet[1];
        }
        retDetail.writeAfterDetail();
        return retDetail;
    }

    //d_1908_b2e0009 对公
    private RetAtsSingleBat d_1908BAT_b2e0009(ReqSingleBat p_req1908BAT) throws Exception {
        RetAtsSingleBat retDetail = new RetAtsSingleBat(p_req1908BAT.reqHeader);
        retDetail.totalAmt = p_req1908BAT.totalAmt;
        retDetail.totalNum = p_req1908BAT.totalAmt;
        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0009";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        //Date tmpDateNow = new Date();
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        xmlWriter.writeElementClosed("ceitinfo");
        if (StringHelper.hasAnyChar(getBankConfig().getValueAsString("transtype"))) {
            xmlWriter.writeElementString("transtype", getBankConfig().getValueAsString("transtype"));
        } else {
            xmlWriter.writeElementClosed("transtype");
        }
        while (p_req1908BAT.readRecord()) {
            ReqPayRecDetail tmpDetail = p_req1908BAT.readedDetail;
            XElement tmpXElement = new XElement(bankHeader.trncod + "-rq", "");
            tmpXElement.addChild(new XElement("insid", tmpDetail.askNumber));

            XElement fractnXElement = new XElement("fractn", "");
            fractnXElement.addChild(new XElement("fribkn", tmpDetail.payArea));
            fractnXElement.addChild(new XElement("actacn", tmpDetail.payAct));
            fractnXElement.addChild(new XElement("actnam", tmpDetail.payName));
            tmpXElement.addChild(fractnXElement);

            XElement toactnXElement = new XElement("toactn", "");
            if (tmpDetail.sameBnk.equals("1")) {
                toactnXElement.addChild(new XElement("toibkn", tmpDetail.recArea));
            } else {
                toactnXElement.addChild(new XElement("toibkn", tmpDetail.cnapsCode));
            }
            toactnXElement.addChild(new XElement("actacn", tmpDetail.recAct));
            toactnXElement.addChild(new XElement("toname", tmpDetail.recName));
            toactnXElement.addChild(new XElement("toaddr", ""));
            toactnXElement.addChild(new XElement("tobknm", tmpDetail.getOppBankLocation()));
            tmpXElement.addChild(toactnXElement);

            tmpXElement.addChild(new XElement("trnamt", tmpDetail.payAmount));
            tmpXElement.addChild(new XElement("trncur", this.convertCurToBank(tmpDetail.payCur)));
            if (tmpDetail.paySpd.equals("1")) {
                tmpXElement.addChild(new XElement("priolv", "1"));
            } else {
                tmpXElement.addChild(new XElement("priolv", "0"));
            }

            tmpXElement.addChild(new XElement("furinfo", tmpDetail.purpose));
            tmpXElement.addChild(new XElement("trfdate", Format.Date8Format.format(tmpDetail.getPayDateTime())));
            //xmlWriter.writeElementClosed("trftime");
            tmpXElement.addChild(new XElement("comacn", tmpDetail.payAct));
            nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);

            //XElement tmpXElement = new XElement("DCOPDPAYX","");
            //tmpXElement.addChild(new XElement("YURREF",p_req1908.singleDetail.askNumber));
            //xmlWriter.writeXElement(tmpXElement);
        }
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                if (isActiveRetCode(tmprspcod)) {
                    //retDetail.respCode = "0";
                    //retDetail.respInfo = tmpRetMsg;
                    //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                    retDetail.batRespCode = "0";
                    retDetail.batRespInfo = tmprspmsg;
                    retDetail.reqBatchNO = p_req1908BAT.reqBatchNO;
                    while (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                        RetAtsSglDetail tmpDetail = new RetAtsSglDetail();
                        tmpDetail.respCode = "0";
                        tmpDetail.transState = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                        tmpDetail.payInfoCode = tmpDetail.transState;
                        tmpDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                        tmpDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                        retDetail.writeRetDetail(tmpDetail);
                    }
                    if (retDetail.getWrittenRdCount() == 0) {
                        retDetail.batTransState = tmprspcod;
                        retDetail.batPayInfoCode = retDetail.batTransState;
                    }
                } else {
                    retDetail.batRespCode = "-1";
                    retDetail.batTransState = tmprspcod;
                    if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                        tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                    }
                    retDetail.batRespInfo = tmprspmsg;
                }
            } else {
                retDetail.batRespCode = "-1";
                retDetail.batRespInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.batRespCode = "0";
            retDetail.batRespInfo = tmpStrRet[1];
            retDetail.reqBatchNO = p_req1908BAT.reqBatchNO;
            retDetail.batTransState = PaymentState.FAIL_Payment;
            retDetail.batPayInfoCode = tmpStrRet[0];
        } else {
            retDetail.batRespCode = "-1";
            retDetail.batRespInfo = tmpStrRet[1];
        }
        retDetail.writeAfterDetail();
        return retDetail;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        //返回ATS的对象
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);

        BOC01Header bankHeader = new BOC01Header();
        bankHeader.trncod = "b2e0007";//交易代码
        bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeStartElement("trn-" + bankHeader.trncod + "-rq");
        while (p_req1944BAT.readRecord()) {
            xmlWriter.writeStartElement(bankHeader.trncod + "-rq");
            xmlWriter.writeElementString("insid", p_req1944BAT.readedDetail.reqSeqID);
            xmlWriter.writeElementClosed("obssid");
            xmlWriter.writeEndElement();
        }

        xmlWriter.writeEndDocument();

        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        //retDetail.respCode = "0";
                        //retDetail.respInfo = tmpRetMsg;
                        //retDetail.reqSeqID = tmpBkReader.getFirstText("/CMS/eb/pub/fSeqno");
                        retAts.batRespCode = "0";
                        retAts.batRespInfo = tmprspmsg;
                        retAts.reqBatchNO = p_req1944BAT.reqBatchNO;
                        while (tmpBkReader.read("/bocb2e/trans/trn-" + bankHeader.trncod + "-rs/" + bankHeader.trncod + "-rs")) {
                            String tmpRetCode = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                            String tmpRetInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");

                            RetAtsSglDetail tmpDetail = new RetAtsSglDetail();
                            tmpDetail.respCode = "0";
                            tmpDetail.transState = tmpRetCode;
                            tmpDetail.payInfoCode = tmpDetail.transState;
                            tmpDetail.respInfo = tmpRetInfo;
                            tmpDetail.reqSeqID = tmpBkReader.readedXElement.getSingleText("insid");
                            retAts.writeRetDetail(tmpDetail);
                        }
                        if (retAts.getWrittenRdCount() == 0) {
                            retAts.batTransState = tmprspcod;
                            retAts.batPayInfoCode = retAts.batTransState;
                        }
                    } else {
                        retAts.batRespCode = "-1";
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retAts.batRespInfo = tmprspmsg;
                    }
                } else {
                    retAts.batRespCode = "-1";
                    retAts.batRespInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.batRespCode = "-1";
                retAts.batRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retAts.batRespCode = "-1";
            retAts.batRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}
